package com.gree.scmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gree.common.entity.SysLogininfor;
import com.gree.common.entity.SysOperLog;
import com.gree.common.entity.TablePageData;
import com.gree.common.utils.StringUtils;
import com.gree.framework.entity.CusUserDetails;
import com.gree.framework.entity.SysUser;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.LogInfoDao;
import com.gree.scmservice.dao.OperLogDao;
import com.gree.scmservice.entity.saas.SaasSysAddon;
import com.gree.scmservice.service.GscmSysOperLogService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 180686 on 2021/3/22 14:47
 */

@Service
public class GscmSysOperLogServiceImpl implements GscmSysOperLogService {

    @Autowired
    private OperLogDao operLogDao;

    @Autowired
    private Dao dao;

    @Override
    public TablePageData<SysOperLog> selectOperLogList(SysOperLog sysOperLog) {
        return operLogDao.displayOperLog(sysOperLog);
    }

    @Override
    public TablePageData<Record> selectOperLogList(JSONObject jsonObject) {
        CusUserDetails cusUserDetails = SecurityUtils.getUser();
        SysUser sysUser = cusUserDetails.getUser();
        String comp = sysUser.getComp();
        String filter = "";
        if(!"".equals(jsonObject.getString("title")) && jsonObject.getString("title")!=null){
            filter+=" and oper.title like '%"+jsonObject.getString("title")+"%'";
        }
        if(!"".equals(jsonObject.getString("business_type")) && jsonObject.getString("business_type")!=null){
            filter+=" and oper.business_type = '"+jsonObject.getString("business_type")+"'";
        }
        if(!"".equals(jsonObject.getString("oper_name")) && jsonObject.getString("oper_name")!=null){
            filter+=" and oper.oper_name like '%"+jsonObject.getString("oper_name")+"%'";
        }
        if(!"".equals(jsonObject.getString("comp")) && jsonObject.getString("comp")!=null){
            filter+=" and oper.comp = '"+jsonObject.getString("comp")+"'";
        }
        if(jsonObject.getJSONArray("dateRange")!=null){
            if(jsonObject.getJSONArray("dateRange").size()!=0){
                filter+=" and oper.oper_time between " +
                        "to_date('"+jsonObject.getJSONArray("dateRange").get(0)+"','yyyy-mm-dd') " +
                        "and to_date('"+jsonObject.getJSONArray("dateRange").get(1)+"','yyyy-mm-dd')";
            }
        }
        filter+=" order by oper.id desc";
        int count = operLogDao.getOperLogCount(comp,filter);
        TablePageData<Record> recordTablePageData = operLogDao.displayOperLog(comp, filter, count, jsonObject.getIntValue("page"), jsonObject.getIntValue("pagesize"));
        return recordTablePageData;
    }

    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return operLogDao.deleteOperinforByIds(operIds);
    }

    @Override
    public void cleanOperLog() {
        operLogDao.cleanOperinfor();
    }
}
